import { useSearchParams } from 'react-router-dom'
import { useRequest } from 'ahooks'
import { questionListAPI } from '@/server/question'

/**
 *  获取问卷列表信息
 */
const useLoadQuestionListData = (param?: {
  isStar?: boolean
  isDeleted?: boolean
  page?: number
  pageSize?: number
}) => {
  const { isStar, isDeleted } = param || {}
  const [searchParams] = useSearchParams()
  return useRequest(
    async () => {
      // 定义查询参数
      const params = {
        search: searchParams.get('search') || '',
        page: parseInt(searchParams.get('page') || '') || 1,
        pageSize: parseInt(searchParams.get('pageSize') || '') || 10,
        isStar,
        isDeleted,
      }
      const { data } = await questionListAPI(params)
      return data
    },
    {
      refreshDeps: [searchParams], // 刷新依赖
    }
  )
}

export default useLoadQuestionListData
